package com.bjpowernode.commons;

import com.bjpowernode.model.TDicType;
import com.bjpowernode.model.TDicValue;
import com.bjpowernode.model.TProduct;
import com.bjpowernode.service.DicTypeService;
import com.bjpowernode.service.ProductService;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter;
import org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseApplication implements CommandLineRunner {

    //入口filter
    FilterChainProxy filterChainProxy;

    //生成登录页面的Filter
    DefaultLoginPageGeneratingFilter loginPageGeneratingFilter;

    //生成退出页面的Filter；
    DefaultLogoutPageGeneratingFilter logoutPageGeneratingFilter;

    //默认的用户名和密码配置
    SecurityProperties securityProperties;

    //登录处理的Filter
    UsernamePasswordAuthenticationFilter authenticationFilter;

    //这是登录后的用户信息对象
    UsernamePasswordAuthenticationToken token;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private DicTypeService tDicTypeService;

    @Resource
    private ProductService productService;

    public static final Map<String, Object> cahceMap = new HashMap<>();

    @Override
    public void run(String... args) throws Exception {
        //key的序列化改成了字符串方式
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //值的序列化方式默认是JdkSerializationRedisSerializer序列化

        //值的序列化改成了json序列化方式
        ObjectMapper mapper = new ObjectMapper();//对象映射工具类， 可以实现 java对象 <----> json对象 的相互转化
        //设置对象里面的字段的可见性
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);// 可见度; 能见度; 能见距离; 可见性; 明显性;
        //设置多态行为的
        mapper.activateDefaultTyping(mapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.EVERYTHING);
        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(mapper, Object.class)); //需要jackson的jar支持


        //hash key的序列化改成了字符串方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        //hash值的序列化改成了json序列化方式
        redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<Object>(mapper, Object.class)); //需要jackson的jar支持

        //查询一下字段值数据，缓存到内存中
        List<TDicType> allDicType = tDicTypeService.getAllDicType();
        allDicType.forEach(tDicType -> {
            String typeCode = tDicType.getTypeCode();
            List<TDicValue> tDicValueList = tDicType.getTDicValueList();

            cahceMap.put(typeCode, tDicValueList);
        });
        System.out.println(cahceMap);

        //查询一下产品数据,把产品数据放入内存的map中
        List<TProduct> tProductList = productService.getAllProduct();
        cahceMap.put(DicEnum.PRODUCT.getCode(), tProductList);
    }
}
